<?php
// Offkai Controller
// $Id: OffkaiController.php 2 2009-12-25 09:19:58Z mocapapa@g.pugpug.org $
class OffkaiController extends CController
{
	/**
	 * @var string specifies the default action to be 'list'.
	 */
	public $defaultAction='menu';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
		);
	}

        /**                                                                                                   
         * Declares class-based actions.                                                                      
         */
        public function actions()
        {
          return array(
                       'captcha'=>array(
                                        'class'=>'CaptchaAction',
                                        ),
                       );
        }

	/**
	 * Select Offkai
	 */
	public function actionMenu()
	{
		$offkai=new Offkai;
		if(isset($_POST['Offkai'])) {
			$offkai->attributes=$_POST['Offkai'];
			if (isset($_POST['createbutton'])) {
				if ($offkai->validate('add_check')) {
					$this->redirect(array('offkai/add', 'offkaicode'=>$offkai->offkaicode));
				}
			} else {
				if ($offkai->validate('menu')) {
					$this->redirect(array('offkai/edit', 'offkaicode'=>$offkai->offkaicode));
				}
			}
		}
		$this->render('menu',array('offkai'=>$offkai));
	}

	/**
	 * Creates a new offkai.
	 * If creation is successful, the browser will be redirected to the 'offkai/menu' page.
	 */
	public function actionAdd()
	{
		$offkai=new Offkai;
                $offkai->offkaicode=$_GET['offkaicode'];

		if(isset($_POST['Offkai']))
		{
			$offkai->attributes=$_POST['Offkai'];
			if($offkai->save())
				$this->redirect(array('offkai/menu','offkaicode'=>$offkai->offkaicode));
		}
		$this->render('add',array('offkai'=>$offkai));
	}

	/**
	 * Updates a particular offkai.
	 * If update is successful, the browser will be redirected to the 'offkai/menu' page.
	 */
	public function actionEdit()
	{
		$offkai=Offkai::model()->findByPk($_GET['offkaicode']);
                $offkai->dbpassword=$offkai->password;
                $offkai->password='';

		if(isset($_POST['Offkai']))
		{
			$offkai->attributes=$_POST['Offkai'];
			if($offkai->save())
				$this->redirect(array('offkai/menu','offkaicode'=>$offkai->offkaicode));
		}
		$this->render('edit',array('offkai'=>$offkai));
	}
}
